package baiqitun.stupro.api.controller;

import baiqitun.stupro.common.model.Result;
import baiqitun.stupro.common.util.ResultUtil;
import baiqitun.stupro.common.validate.group.SaveGroup;
import baiqitun.stupro.security.entity.SysUser;
import baiqitun.stupro.security.service.SysUserService;
import baiqitun.stupro.subject.entity.SysStudentInfo;
import baiqitun.stupro.subject.service.SysStudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author kedaji
 */
@Api(description = "学生控制器")
@RequestMapping("/studentInfo")
@RestController
public class StudentInfoController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysStudentInfoService sysStudentInfoService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("向导师发送【选择导师】的申请")
    @PostMapping("/selectTutorRequest")
    public Result<?> sendSelectTutorRequest(Authentication authentication,
                                            @ApiParam("导师id") @NotBlank(message = "导师id不能为空") String tutorId,
                                            @ApiParam("备注,") String description){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        SysUser user = sysUserService.getUserByUsername(userDetails.getUsername());
        sysStudentInfoService.sendSelectTutorRequest(user.getId(), tutorId, description);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("撤回向导师发送【选择导师】的申请")
    @DeleteMapping("/selectTutorRequest")
    public Result<?> withinSelectTutorRequest(@ApiParam("任务id") @NotBlank(message = "申请id不能为空") String requestId,
                                                      @ApiParam("备注,非必填") String description){
        sysStudentInfoService.withinSelectTutorRequest(requestId, description);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("向导师发送【撤销选择当前导师】的申请")
    @PostMapping("/cancelSelectedTutorRequest")
    public Result<?> sendCancelSelectedTutorRequest(Authentication authentication,
                                                    @ApiParam("备注，必填") @NotBlank(message = "备注不能为空") String description){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        SysUser user = sysUserService.getUserByUsername(userDetails.getUsername());
        sysStudentInfoService.sendCancelSelectedTutorRequest(user.getId(), description);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("撤回向导师发送的【取消选择当前导师】申请")
    @DeleteMapping("/cancelSelectedTutorRequest")
    public Result<?> withinCancelSelectedTutorRequest(@ApiParam("任务id") @NotBlank(message = "taskId不能为空") String taskId,
                                                      @ApiParam("备注,非必填") String description){
        sysStudentInfoService.withinSelectTutorRequest(taskId, description);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("向指定部门（班级）添加一个学生")
    @PostMapping
    public Result<?> save(Authentication authentication,
                                       @ApiParam("用户信息") @Validated(SaveGroup.class) SysUser user,
                                       @ApiParam("学生信息") @Validated(SaveGroup.class) SysStudentInfo studentInfo,
                                       @ApiParam("所在系的id") @NotBlank(message = "部门名称不能为空") String departId){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        user.setCreateBy(userDetails.getUsername());
        studentInfo.setCreateBy(userDetails.getUsername());
        sysStudentInfoService.saveToDepart(user, studentInfo, departId);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("获取学生当前等待导师处理的任务")
    @GetMapping("/selectTutorRequest")
    public Result<List<Task>> listTaskForSelectTutorRequest(Authentication authentication){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        SysUser user = sysUserService.getUserByUsername(userDetails.getUsername());
        List<Task> tasks = sysStudentInfoService.listTaskForSelectTutorRequest(user.getId());
        return ResultUtil.ok(tasks);
    }
}
